<?php
/**
 * minf - core library
 * modules/detect.php
 *
 * @author per function || minf.org team
 * @version 0.beta4
 * @copyright no,no,left, minf.org, 25 February, 2009
 * @license GPL
 * @package default
 **/

/**
 * Returns Server Software, simple, lowercase,
 *  just name : eg. apache, lighttpd
 *
 * @return String ServerName
 */
function server_software() {
	$srv = $_SERVER['SERVER_SOFTWARE'];		// Apache/2.2.3 (Debian) PHP/5.2.0-8+etch11
											// lighttpd/1.4.19
	// Destroy after '/' symbol, and lower the case
	if ($srv) $srv = strtolower(substr($srv,0,strpos($srv, '/')));
	
	return $srv;
}

/**
 * Detects request details for minf
 *  uses core/modules/string.php @ minf
 *
 * @return Array Request Data
 */
function user_request() {
	// Needs a Fix
	// when path is : /Users/furkanmustafa/Sites/minf3
	// and user is : /~furkanmustafa/minf3/index.php
	// is lacks probing it : thinks linkreq : anmustafa/minf3/
	$c = array();
	$srv = server_software();
	if ($srv=="apache") {
		//if (str_matchEnding($_SERVER["SCRIPT_FILENAME"], "/index.php")) 
		if (isset($_SERVER['PATH_INFO'])) if (strlen($_SERVER['PATH_INFO'])>=1) $_GET['q'] = $_SERVER['PATH_INFO'];
		$c["rootpath"] = dirname($_SERVER["SCRIPT_FILENAME"]);
		$c["requrl"] = $_SERVER["REQUEST_URI"];
		$c["script"] = $_SERVER["SCRIPT_NAME"];
		$c["method"] = $_SERVER["REQUEST_METHOD"];
		$c["acceptlang"] = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
		$c["docroot"] = $_SERVER["DOCUMENT_ROOT"];
		$c["get"] = $_GET;
	} elseif ($srv=="lighttpd" || $srv=="fcgi") {
		//if (str_matchEnding($_ENV["SCRIPT_FILENAME"], "/index.php")) 
		$c["rootpath"] = dirname($_ENV["SCRIPT_FILENAME"]);
		$c["requrl"] = $_ENV["REQUEST_URI"];
		$c["script"] = $_ENV["SCRIPT_NAME"];
		$c["method"] = $_ENV["REQUEST_METHOD"];
		$c["acceptlang"] = $_ENV["HTTP_ACCEPT_LANGUAGE"];
		$c["docroot"] = $_ENV["DOCUMENT_ROOT"];
		$c["get"] = $_GET;
	}
	
	if (defined('DOCROOT')) 
		$c['docroot'] = constant('DOCROOT');
	
	// these must be adapted to lighttpd (below)
	
	$c["proto"] = (strtoupper($_SERVER["HTTPS"])=="ON") ? 'https' : 'http';
	
	$c["domain"] = $_SERVER["SERVER_NAME"];
	$c["portadd"] = substr($_SERVER["HTTP_HOST"], strlen($c['domain']));
	
	// there is no $_SERVER or $_ENV below (theyre all ok)
	
	$c["relurl"] = substr($c["rootpath"], strlen($c["docroot"]));
	if (strpos($c["requrl"], "?")===false)
		$c["linkreq"] = substr($c["requrl"], strlen($c["relurl"]) + 1);
	else
		$c["linkreq"] = substr($c["requrl"], strlen($c["relurl"]) + 1, strpos($c["requrl"], "?") - strlen($c["relurl"]) - 1);
	$ipos = strpos($c["linkreq"], "index.php");
	if ($ipos!==false) if ($ipos==0) $c["linkreq"] = substr($c["linkreq"], 9);
	
	if (!$c["linkreq"] || $_GET["q"]) { $c["urlstyle"]="get"; $c["linkreq"] = $_GET["q"]; } else { $c["urlstyle"]="rewrite"; }
	
	if (!$c["urlstyle"]) $c["urlstyle"] = "nomatter";
	if (!$c["linkreq"]) $c["linkreq"] = "";
	
	$c["rooturl"] = $c["proto"]."://".$c["domain"].$c["portadd"].$c["relurl"];
	
	return $index ? $c[$index] : $c;
}

?>